const mongoose = require("mongoose")
const EssayModel = require("../models/EssayModel")
const MusicModel = require("../models/MusicModel.js")
const VideoMidel = require("../models/VideoModel.js")
const AlbumModel = require("../models/AblumsModel.js")
const SpeaksModel = require("../models/SpeaksModel.js")
const AdminUserModel = require("../models/AdminUserModel.js")
const MessageModel = require("../models/MessageModel")
const WebService = {
    find_essay_list: async (type, skp) => {
        return await EssayModel.find({
            Essay_type: type
        }).skip(skp).limit(6).sort({ publish_time: "desc" }).select('-__v -end_time -rich_text')
    },
    find_music_list: async (skp) => {
        return await MusicModel.find({}).sort({ publish_time: "desc" }).skip(skp).limit(8).select('-__v')
    },
    find_one: async (skp) => {
        return {
            music_one: await MusicModel.findOne().skip(skp),
            count: await MusicModel.countDocuments()
        }
    },
    find_video_list: async (id) => {
        return await VideoMidel.find().skip(id).limit(6).select('-__v')
    },
    find_essay_recommend: async () => {
        return await EssayModel.find().limit(2).sort({ review: 'desc' })
    },
    findOneEssay: async (id) => {
        return await EssayModel.findOne({
            _id: id
        })
    },
    findOneVideo: async (id) => {
        return await VideoMidel.findOne({
            _id: id
        }).select('-__v')
    },
    find_images: async (data) => {
        return await AlbumModel.find({
            ...data
        }).sort({ day: "desc" }).select('-__v')
    },
    find_talk: async (skp = 0, year) => {
        if (Number(year) === 0) {
            return await SpeaksModel.find().skip(skp).limit(6).sort({ publish_time: 'desc' }).select('-__v')
        } else {
            return await SpeaksModel.find({
                year: year
            }).skip(skp).limit(6).sort({ publish_time: 'desc' }).select('-__v')
        }

    },
    speak_diffrent_year: async () => {
        return await SpeaksModel.distinct('year')
    },
    ablums_diffrent_year: async () => {
        return await AlbumModel.distinct('year')
    },
    find_admin: async () => {
        return await AdminUserModel.findOne({
            role: 0
        }).select('-password -__v -school -english_name')
    },
    find_count: async () => {
        return {
            essay_count: await EssayModel.aggregate([
                {
                    $group: {
                        _id: "$Essay_type",
                        count: { $sum: 1 }
                    }
                }
            ]),
            speak_count: await SpeaksModel.countDocuments()
        }
    },
    message_img: async (data) => {
        return await MessageModel.create({
            ...data
        })
    },
    get_messages: async () => {
        return await MessageModel.find({}).limit(100).sort({ publish_time: "desc" })
    },
    //点击文章浏览自增一
    add_review: async (id) => {
        return await EssayModel.updateOne({
            _id: id
        }, {
            $inc: { browse: 1 }
        })
    },
    add_discuss: async (id) => {
        return await EssayModel.updateOne({
            _id: id
        }, {
            $inc: { review: 1 }
        })
    },
    add_video_review: async (id) => {
        return await VideoMidel.updateOne({
            _id: id
        }, {
            $inc: { view: 1 }
        })
    },
    add_video_disc: async (id) => {
        return await VideoMidel.updateOne({
            _id: id
        }, {
            $inc: { comment: 1 }
        })
    },
    search_essays: async (word) => {
        const data = await EssayModel.aggregate([
            {
                $match: { title: { $regex: word, $options: 'i' } }
            },
            {
                $limit: 10
            },
            {
                $project: {
                    title: 1,
                    _id: 1,
                    Essay_type: 1

                }
            }

        ])
        return data

    },

    //测试
    test: async () => {
        return await EssayModel.aggregate([
            {
                $match: { _id: new mongoose.Types.ObjectId("6659d2445499f787b2602db9") }
            },
            {
                $addFields: {
                    _strId: { $toString: '$_id' }
                }
            },

            {
                $lookup: {
                    from: "discusses",
                    localField: "_strId",
                    foreignField: 'article_id',
                    as: "comments"
                }
            },
            {
                $addFields: {
                    count: { $size: '$comments' }
                }
            },
            {
                $project: {
                    count: 1
                }
            }
        ])
    }

}


// { $match: { articleId: mongoose.Types.ObjectId(articleId) } },

module.exports = WebService